from django.db import models
from django.db.models import Q

# Create your models here.

class user(models.Model):
    seq = models.AutoField(primary_key=True)
    uid = models.CharField(max_length=16, db_index=True, unique=True)
    name = models.CharField(max_length=64, db_index=True)
    familyname = models.CharField(max_length=40)
    lastname = models.CharField(max_length=40)
    sex = models.CharField(max_length=4)
    age = models.IntegerField()
    job = models.CharField(max_length=64)
    location = models.CharField(max_length=30)
    interests = models.TextField()
    birthday = models.DateField()
    info = models.TextField()
    mail = models.CharField(max_length=255)
    done = models.BooleanField(default=False, db_index=True)
    modtime = models.DateTimeField()
    numclasses = models.IntegerField()
    avatar = models.CharField(max_length=250)

    @classmethod
    def getArrBySeq1(cls, seqs):
        condition = None
        for s in seqs:
            if not condition:
                condition = Q(seq = s)
            else:
                condition = (condition | Q(seq = s))
        if condition == None:
            return []
        res = cls.objects.using('crawlmsn').filter(condition)
        ret = []
        for s in seqs:
            bingo = False
            for u in res:
                if u.seq == s:
                    bingo=True
                    ret.append(u)
                    #res.remove(u) 
                    break
            if not bingo:
                ret.append(None)
        return ret


    def __unicode__(self):
        return ("A record in table 'user' seq(%d)" % self.seq) + " uid(" + self.uid + ")" 

    class Admin:
        pass

class blog(models.Model):
    seq = models.AutoField(primary_key=True, db_index=True)
    uid = models.CharField(max_length=16, db_index=True) 
    blogid = models.IntegerField(db_index=True)
    title = models.CharField(max_length=1024)
    url = models.CharField(max_length=512, db_index=True)
    time = models.DateField(db_index=True)
    num_comment = models.IntegerField()
    blog_type = models.CharField(max_length=255)
    content = models.TextField()
    plaintext = models.TextField()
    cat_id = models.IntegerField()
    cat_name = models.CharField(max_length=32)

    @classmethod
    def getArrBySeq1(cls, seqs):
        condition = None
        for s in seqs:
            if not condition:
                condition = Q(seq = s)
            else:
                condition = (condition | Q(seq = s))
        if condition == None:
            return []
        res = cls.objects.using('crawlmsn').filter(condition)
        ret = []
        for s in seqs:
            bingo = False
            for u in res:
                if u.seq == s:
                    bingo=True
                    ret.append(u)
                    #res.remove(u) 
                    break
            if not bingo:
                ret.append(None)
        return ret

    def __unicode__(self):
        return ("A Record in table 'blog' seq(%d)" % self.seq) + " blog_id(" + str(self.blogid) + ") title(" + self.title + ")"

    class Admin:
        pass

class usernoblog(models.Model):
    uid = models.CharField(max_length=16, db_index=True)
    name = models.CharField(max_length=64, db_index=True)
    modtime = models.DateTimeField(db_index=True)

    def __unicode__(self):
        return "A Record in table 'usernoblog' uid(" + self.uid + ") modtime(" + self.modtime + ")"
    class Admin:
        pass

class seeds(models.Model):
    name = models.CharField(max_length=64, db_index=True)
    done = models.BooleanField(default=False, db_index=True)

    def __unicode__(self):
        return "name:(" + self.name + "), done:(" + self.done + ")"
    class Admin:
        pass

class userpie(models.Model):
    seq = models.AutoField(primary_key=True)
    uid = models.CharField(max_length=20, db_index=True)
    cat_id = models.IntegerField(db_index=True)
    cat_name = models.CharField(max_length=32, db_index=True)
    blog_ids = models.TextField()
    keywords = models.CharField(max_length=256)

    @classmethod
    def getArrByUid1(cls, uids):
        condition = None
        for u in uids:
            if not condition:
                condition = Q(uid = u)
            else:
                condition = (condition | Q(uid = u))
        if condition == None:
            return []
        res = cls.objects.using('crawlmsn').filter(condition)
        ret = {}
        for u in uids:
            bingo = False
            for up in res:
                if up.uid == u:
                    bingo = True
                    if ret.has_key(u):
                        ret[u].append(up)
                    else:
                        ret[u]=[up]
                    #res.remove(up)
            if not bingo:
                ret[u] = []
        return ret

    def __unicode__(self):
        return "A Record in table 'userpie' uid(%s) cat_id(%d) blogs(%s)" % (self.uid, self.cat_id, self.blog_ids)
    class Admin:
        pass
    
